package com.tc.workflow;

import com.tc.support.BeanMapper;
import com.tc.workflow.service.FormHeaderService;
import com.vip.vpal.workflow.WorkflowEngine;
import com.vip.vpal.workflow.entity.FormHeader;
import com.vip.vpal.workflow.model.PageForm;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

//FormHeader 可自定义
@Service
public class FormHeaderServiceImpl<B> extends SimplePageFormManager<FormHeader, B> implements FormHeaderService<B> {
    private final Logger logger = LoggerFactory.getLogger(FormHeaderServiceImpl.class);
    @Autowired
    public FormHeaderServiceImpl(WorkflowEngine workflowEngine) {super(workflowEngine);}
    @Autowired
    private BeanMapper beanMapper;

    @Override
    public FormHeader save(FormHeader formHeaderModel, B body) throws Exception {
        PageForm<FormHeader, B> form = new PageForm();
        form.setHeader(beanMapper.map(formHeaderModel, FormHeader.class));
        form.setBody(body);
        form.setAction("OPERATE_ACCEPT").setComments("清算审核");
        if (formHeaderModel.getId() != null) {
            form.setNewForm(false);
        }
        try {
            this.handleWithoutWorkflow(form);
            return beanMapper.map(form.getHeader(), FormHeader.class);
        } catch (Exception e) {
            logger.info("商户入网保存 更新流水失败", e);
            throw e;
        }
    }

    @Override
    public FormHeader saveWithWorkflow(FormHeader formHeaderModel, B body) throws Exception {
        PageForm<FormHeader, B> form = new PageForm();
        form.setHeader(beanMapper.map(formHeaderModel, FormHeader.class));
        form.setBody(body);
        form.setAction("OPERATE_ACCEPT").setComments("清算审核");
        if (formHeaderModel.getId() != null) {
            form.setNewForm(false);
        }
        try {
            this.handleWithWorkflow(form);
            return beanMapper.map(form.getHeader(), FormHeader.class);
        } catch (Exception e) {
            logger.info("商户入网保存 更新流水失败", e);
            throw e;
        }
    }


}